package com.example.springboot.ytest.ytool;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

public class DateUtilDs {
    protected static final transient Log log = LogFactory.getLog(DateUtilDs.class);
    private static final int[] dayArray = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static final String FORMAT_TIME = "HH:mm:ss";
    public static final int HOUR = 10;
    public static final int DAY = 11;
    public static final int MINUTE = 12;
    public static final int SECOND = 13;
    public static final int MILLISECOND = 14;
    public static long millionSecondsOfDay = 86400000L;

    public DateUtilDs() {
    }

    public static Date convertUtilDateToSQLDate(java.util.Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            Date jd = new Date(cl.getTimeInMillis());
            return jd;
        }
    }

    public static java.util.Date convertSQLDateToUtilDate(Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            java.util.Date jd = new java.util.Date(cl.getTimeInMillis());
            return jd;
        }
    }

    public static java.util.Date stringToDate(String date) {
        if (date != null && !"".equalsIgnoreCase(date)) {
            Calendar cd = Calendar.getInstance();
            StringTokenizer token = new StringTokenizer(date, "-/ :.");
            if (token.hasMoreTokens()) {
                cd.set(1, Integer.parseInt(token.nextToken()));
            } else {
                cd.set(1, 1970);
            }

            if (token.hasMoreTokens()) {
                cd.set(2, Integer.parseInt(token.nextToken()) - 1);
            } else {
                cd.set(2, 0);
            }

            if (token.hasMoreTokens()) {
                cd.set(5, Integer.parseInt(token.nextToken()));
            } else {
                cd.set(5, 1);
            }

            if (token.hasMoreTokens()) {
                cd.set(11, Integer.parseInt(token.nextToken()));
            } else {
                cd.set(11, 0);
            }

            if (token.hasMoreTokens()) {
                cd.set(12, Integer.parseInt(token.nextToken()));
            } else {
                cd.set(12, 0);
            }

            if (token.hasMoreTokens()) {
                cd.set(13, Integer.parseInt(token.nextToken()));
            } else {
                cd.set(13, 0);
            }

            if (token.hasMoreTokens()) {
                cd.set(14, Integer.parseInt(token.nextToken()));
            } else {
                cd.set(14, 0);
            }

            return cd.getTime();
        } else {
            return null;
        }
    }

    public static String dateToString(java.util.Date date) {
        if (date == null) {
            return "";
        } else {
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            return cl.get(1) + "-" + (cl.get(2) + 1) + "-" + cl.get(5);
        }
    }

    public static String dateTo0String(java.util.Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date).trim();
    }

    public static java.util.Date getFirstDateOfCurrentMonth() {
        Calendar cl = Calendar.getInstance();
        cl.set(5, 1);
        return cl.getTime();
    }

    public static java.util.Date getFirstDateOfMonth(String date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(stringToDate(date));
        cl.set(5, 1);
        return cl.getTime();
    }

    public static java.util.Date getLastDateOfMonth(String date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(stringToDate(date));
        int year = cl.get(1);
        int month = cl.get(2) + 1;
        if (month >= 1 && month <= 12) {
            int day;
            if (month == 2) {
                if (isLeapYear(year)) {
                    day = 29;
                } else {
                    day = dayArray[month - 1];
                }
            } else {
                day = dayArray[month - 1];
            }

            cl.set(5, day);
            return cl.getTime();
        } else {
            return null;
        }
    }

    public static boolean isLeapYear(int year) {
        if (year % 400 == 0) {
            return true;
        } else if (year % 4 == 0) {
            return year % 100 != 0;
        } else {
            return false;
        }
    }

    public static java.util.Date getDateTimeBeforeNow(int type, int value) {
        return getDateTimeBefore(new java.util.Date(), type, value);
    }

    public static java.util.Date getDateTimeBefore(java.util.Date date, int type, int value) {
        long time = date.getTime() / 1000L;
        switch(type) {
        case 10:
            time -= (long)(3600 * value);
            break;
        case 11:
            time -= (long)(86400 * value);
            break;
        case 12:
            time -= (long)(60 * value);
            break;
        case 13:
            time -= (long)value;
        }

        return new java.util.Date(time * 1000L);
    }

    public static java.util.Date getDateTimeAfterNow(int type, long value) {
        return getDateTimeAfter(new java.util.Date(), type, value);
    }

    public static java.util.Date getDateTimeAfter(java.util.Date date, int type, long value) {
        long nextTime = date.getTime() / 1000L;
        switch(type) {
        case 10:
            nextTime += 3600L * value;
            break;
        case 11:
            nextTime += 86400L * value;
            break;
        case 12:
            nextTime += 60L * value;
            break;
        case 13:
            nextTime += value;
        }

        return new java.util.Date(nextTime * 1000L);
    }

    public static java.util.Date getNextDate(String date) {
        Calendar cd = Calendar.getInstance();
        StringTokenizer token = new StringTokenizer(date, "-/ :.");
        if (token.hasMoreTokens()) {
            cd.set(1, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(1, 1970);
        }

        if (token.hasMoreTokens()) {
            cd.set(2, Integer.parseInt(token.nextToken()) - 1);
        } else {
            cd.set(2, 0);
        }

        if (token.hasMoreTokens()) {
            cd.set(5, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(5, 1);
        }

        if (token.hasMoreTokens()) {
            cd.set(11, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(11, 0);
        }

        if (token.hasMoreTokens()) {
            cd.set(12, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(12, 0);
        }

        if (token.hasMoreTokens()) {
            cd.set(13, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(13, 0);
        }

        if (token.hasMoreTokens()) {
            cd.set(14, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(14, 0);
        }

        long nextTime = cd.getTimeInMillis() + 86400000L;
        return new java.util.Date(nextTime);
    }

    public static boolean isToday(java.util.Date date) {
        Calendar today = Calendar.getInstance();
        today.setTime(new java.util.Date());
        Calendar day = Calendar.getInstance();
        day.setTime(date);
        return today.get(1) == day.get(1) && today.get(2) == day.get(2) && today.get(5) == day.get(5);
    }

    public static java.util.Date StringToDate(String sDate, int nFormat) {
        new java.util.Date();

        try {
            if (sDate != null && !sDate.equals("")) {
                String sFormat = "";
                if (nFormat == 1) {
                    sFormat = "yyyy-MM-dd";
                } else if (nFormat == 2) {
                    sFormat = "yyyy-MM-dd HH:mm:ss";
                } else if (nFormat == 3) {
                    sFormat = "yyyy��MM��";
                } else if (nFormat == 4) {
                    sFormat = "yyyy-MM-dd HH:mm";
                } else if (nFormat == 5) {
                    sFormat = "HH:mm:ss";
                } else {
                    sFormat = "yyyy-MM-dd";
                }

                SimpleDateFormat simDateFormat = new SimpleDateFormat(sFormat);
                java.util.Date resDate = simDateFormat.parse(sDate);
                return resDate;
            } else {
                return null;
            }
        } catch (Exception var5) {
            log.error("", var5);
            return null;
        }
    }

    public static String DateToString(java.util.Date dDate, int nFormat) {
        String resString = "";

        try {
            String sFormat = "";
            if (nFormat == 1) {
                sFormat = "yyyy-MM-dd";
            } else if (nFormat == 2) {
                sFormat = "yyyy-MM-dd HH:mm:ss";
            } else if (nFormat == 3) {
                sFormat = "yyyy-MM-dd EEE";
            } else if (nFormat == 4) {
                sFormat = "yyyy-MM-dd HH:mm";
            } else {
                sFormat = "yyyy-MM-dd";
            }

            SimpleDateFormat simDateFormat = new SimpleDateFormat(sFormat);
            resString = simDateFormat.format(dDate);
            return resString;
        } catch (Exception var5) {
            return resString;
        }
    }

    public static int getDay(java.util.Date date1, java.util.Date date2) {
        Long d2 = date2.getTime();
        Long d1 = date1.getTime();
        return (int)((d2 - d1) / millionSecondsOfDay);
    }

    public static java.util.Date addDay(java.util.Date date, int days) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.add(5, days);
        return c.getTime();
    }

    public static String format(java.util.Date date, String formater) {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(date);
    }

    public static java.util.Date parse(String date, String formater) {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        java.util.Date result = null;

        try {
            result = sdf.parse(date.trim());
        } catch (ParseException var5) {
            log.error("", var5);
        }

        return result;
    }

    public static int getWeekOfDate(java.util.Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(7) - 1 == 0 ? 7 : calendar.get(7) - 1;
    }

    public static String dateToString(java.util.Date date, String format) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(date).trim();
        }
    }

    public static boolean isDateString(String str) {
        SimpleDateFormat simDateFormat = null;
        String sFormat = "yyyy-MM-dd HH:mm:ss.S";

        try {
            simDateFormat = new SimpleDateFormat(sFormat);
            simDateFormat.parse(str);
            return true;
        } catch (Exception var6) {
            sFormat = "yyyy-MM-dd HH:mm:ss";

            try {
                simDateFormat = new SimpleDateFormat(sFormat);
                simDateFormat.parse(str);
                return true;
            } catch (Exception var5) {
                sFormat = "yyyy-MM-dd";

                try {
                    simDateFormat = new SimpleDateFormat(sFormat);
                    simDateFormat.parse(str);
                    return true;
                } catch (Exception var4) {
                    return false;
                }
            }
        }
    }

    public static String getCurrentlyStringTime() {
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = tempDate.format(new java.util.Date());
        return datetime;
    }

    public static int getAge(java.util.Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        } else {
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        }
    }

    public static int timeCompare(String t1, String t2) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        try {
            c1.setTime(formatter.parse(t1));
            c2.setTime(formatter.parse(t2));
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        int result = c1.compareTo(c2);
        return result;
    }

    public static boolean dateCompare(java.util.Date dat1, java.util.Date dat2) {
        boolean dateComPareFlag = true;
        if (dat2.compareTo(dat1) != 1) {
            dateComPareFlag = false;
        }

        return dateComPareFlag;
    }

    public static java.util.Date getStartDayTime(java.util.Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar start = Calendar.getInstance();
            start.setTime(date);
            start.set(11, 0);
            start.set(12, 0);
            start.set(13, 0);
            return start.getTime();
        }
    }

    public static java.util.Date getToDayLastTime(java.util.Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar today = Calendar.getInstance();
            today.setTime(date);
            today.set(11, 23);
            today.set(12, 59);
            today.set(13, 59);
            return today.getTime();
        }
    }

    public static java.util.Date getYesterday(java.util.Date date) {
        Calendar yesterday = Calendar.getInstance();
        yesterday.setTime(date);
        yesterday.add(5, -1);
        return yesterday.getTime();
    }

    public static java.util.Date getNextDate(java.util.Date date) {
        Calendar today = Calendar.getInstance();
        today.setTime(date);
        today.add(5, 1);
        return today.getTime();
    }

    public static String dateToSimpleString(java.util.Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
    }

    public static String dateToFileSimpleString(java.util.Date date) {
        return (new SimpleDateFormat("yyyyMMdd")).format(date);
    }

    public static void main(String[] args) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date dateTime = df.parse("2012-04-07");
        System.out.println(getStartDayTime(new java.util.Date()));
        Calendar e = Calendar.getInstance();
        dateTime.getTime();
        new Date(1325260800L);
        e.setTimeInMillis(1325260800L);
    }
}
